// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:57:27
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Property\PRODParser.cpp
 */
#include "PRODParser.h"

#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPRODService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PROD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PROD/PROD.md>
*/

// 定义PROD关键字的枚举，表示每个字段的位置索引
enum PRODKey : std::uint8_t
{
    PRODKEY_PID = 2,  // 产品ID字段
    PRODKEY_MID = 3,  // 材料ID字段
    PRODKEY_A   = 4,  // 横截面积字段
    PRODKEY_J   = 5,  // 扭转常数字段
    PRODKEY_C   = 6,  // 扭转应力修正系数字段
    PRODKEY_NSM = 7,  // 非结构质量字段
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParsePROD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的PROD数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::PRODData tempPROD;

    // TODO(bug 001) m_domainId的正确填写，目前默认设置为1
    tempPROD.m_domainId = 1;

    // 分割输入字符串EntryInfo，并将其结果存储到dividedEntryInfo

    int curIDKey = 1;  // 当前处理的字段索引

    // 遍历分割后的字符串条目
    for (auto curStrKey : dividedEntryInfo)
    {
        // 将字符串转换为科学计数法

        // 检查当前字符串是否为空（只包含空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 跳过空字段，继续下一个字段
            continue;
        }

        // 根据当前字段索引解析相应的数据并存储到tempPROD
        switch (curIDKey)
        {
        case PRODKEY_PID:
            // 解析PID字段，将其转换为长整型并存储到m_id
            tempPROD.m_id = convertToI64 (curStrKey);
            break;
        case PRODKEY_MID:
            // 解析MID字段，将其转换为长整型并存储到m_mId
            tempPROD.m_mId = convertToI64 (curStrKey);
            break;
        case PRODKEY_A:
            // 解析横截面积A字段，将其转换为双精度浮点数并存储到m_area
            tempPROD.m_area = convertToReal (curStrKey);
            break;
        case PRODKEY_J:
            // 解析扭转常数J字段，将其转换为双精度浮点数并存储到m_torsionFactor
            tempPROD.m_torsionFactor = convertToReal (curStrKey);
            break;
        case PRODKEY_C:
            // 解析扭转应力修正系数C字段，将其转换为双精度浮点数并存储到m_torsionStressfactor
            tempPROD.m_torsionStressfactor = convertToReal (curStrKey);
            break;
        case PRODKEY_NSM:
            // 解析非结构质量NSM字段，将其转换为双精度浮点数并存储到m_nsm
            tempPROD.m_nsm = convertToReal (curStrKey);
            break;
        default:
            // 如果索引不匹配任何已知字段，跳过
            break;
        }

        curIDKey++;  // 处理完当前字段，移动到下一个字段
    }

    // 将解析后的PROD数据添加到数据库服务中
    return dbServiceFactory->get<IPRODService> ()->Add (tempPROD);
}